package com.example.springbott.service;

import com.example.springbott.bean.MarketComment;
import com.example.springbott.bean.MarketCommentExample;
import com.example.springbott.bean.common.BaseParam;
import com.example.springbott.bean.zeng.AdminCommentListVo;
import com.example.springbott.bean.zeng.CommentList;
import com.example.springbott.bean.zeng.CommentListVo;
import com.example.springbott.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminCommentServiceImpl implements AdminCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public AdminCommentListVo commentList(BaseParam baseParam, CommentList commentList) {
        Integer limit = baseParam.getLimit();
        String order = baseParam.getOrder();
        Integer page = baseParam.getPage();
        String sort = baseParam.getSort();
        Integer userId = commentList.getUserId();
        Integer valueId = commentList.getValueId();

        //分页
        PageHelper.startPage(page, limit);
        //设置条件查询
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        if (baseParam.getSort() != null || baseParam.getOrder() != null) {
            example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        }
        criteria.andDeletedEqualTo(false);
        //拿到评论
        List<MarketComment> comments = marketCommentMapper.selectByExample(example);

        PageInfo info = new PageInfo(comments);
        long total = info.getTotal();
        int pages = info.getPages();

        AdminCommentListVo adminCommentListVo = new AdminCommentListVo();
        adminCommentListVo.setTotal((int) total);
        adminCommentListVo.setPage(page);
        adminCommentListVo.setLimit(limit);
        adminCommentListVo.setPages(pages);
        adminCommentListVo.setList(comments);

        return adminCommentListVo;

    }

    @Override
    public void delete(MarketComment comment) {
        //设置条件查询
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        if (comment.getUserId() != null) {
            criteria.andUserIdEqualTo(comment.getUserId());
        }
        if (comment.getId() != null) {
            criteria.andIdEqualTo(comment.getId());
        }
        criteria.andDeletedEqualTo(false);
        comment.setDeleted(true);
        marketCommentMapper.updateByExampleSelective(comment, example);

    }
}
